package com.jiaoyou.baojian.http;

import com.jiaoyou.baojian.http.response.BaseResponse;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by DemonZYM on 17/5/5.
 */

public abstract class BaofangCallback2<T> implements Callback<BaseResponse<T>> {

    @Override
    public void onResponse(Call<BaseResponse<T>> call, Response<BaseResponse<T>> response) {
        if(!response.isSuccessful())
        {
            onFailure(call, new Throwable(response.message()));
            return;
        }
        if(response.body().status != 200 && response.body().status != 300){
            onFailure(call, new Throwable(response.body().msg));
            return;
        }
        if(response.body().data == null)
        {
            onSuccess(null);
        } else {
            onSuccess(Utils.fromJsonArray(response.body(), (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]).data);
        }
    }

    @Override
    public abstract void onFailure(Call<BaseResponse<T>> call, Throwable t);

    public abstract void onSuccess(List<T> list);
}
